/*
 * Copyright (c) 2013.
 *  游戏服务器核心代码编写人陈磊拥有使用权
 *  联系方式：E-mail:13638363871@163.com ;qq:502959937
 *  个人博客主页：http://my.oschina.net/chenleijava
 */

package com.dc.gameserver.serverCore.service.effective.l7;

import com.dc.gameserver.baseConfig.Config;
import com.dc.gameserver.proto.itemserverprotoVo.attrVoResponse;
import com.dc.gameserver.serverCore.model.dbsql.modelCache.ModelCacheService;
import com.dc.gameserver.serverCore.model.dbsql.modelService.PlayerService;
import com.dc.gameserver.serverCore.model.dbsql.modelVo.TRole;
import com.dc.gameserver.serverCore.model.gamemodel.BookModel;
import com.dc.gameserver.serverCore.model.gamemodel.ItemCell;
import com.dc.gameserver.serverCore.model.gamemodel.Model;
import com.dc.gameserver.serverCore.model.gamemodel.script.ScriptFactory;
import com.dc.gameserver.serverCore.service.battle.GameFormula;
import com.dc.gameserver.serverCore.service.character.PlayerInstance;
import com.dc.gameserver.serverCore.service.effective.abstractEffective;
import javolution.util.FastTable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 石头哥哥 </br>
 *         dcserver1.3 </br>
 *         Date:14-1-8 </br>
 *         Time:下午4:43 </br>
 *         Package:{@link com.dc.gameserver.serverCore.service.effective.l7}</br>
 *         Comment： 穿戴后增加功法可存储的灵气上限   秘籍类
 */
@Service
public class _707_Effective extends abstractEffective {

    @Resource
    private ModelCacheService modelCacheService;

    @Resource
    private PlayerService playerService;

    /**
     * spring 初始化索引
     */
    @Override
    public void PostConstruct() {
        EFFECTIVE_ID[0x2c3]=this;
    }

    /**
     * @param player
     * @param model   book ,equ
     * @param goodsID itemID 装备  秘籍编号
     * @param flag    1.number 2.percent
     * @param add     true false
     * @param builder
     * 	若玩家更换功法书后，
     * 在精舍中已存储在功法书中的灵气值大于更换后功法书的储存上限（包括成套的功法书加成）
     * ，保留之前存储的灵气值，但是不再增加新的灵气。
     *
     * roleStateExperies   灵气值
     * 替换，穿戴，都重新计算功法上限值 以及功法加成
     * 重新计算
     */
    @Override
    public void effect(PlayerInstance player, Model model, int goodsID, int flag, boolean add, attrVoResponse.Builder builder) {
        if (add&&builder!=null){
            BookModel _bookModel= (BookModel) model;
            int roleID=player.getID();
            String role_prefix=ModelCacheService.ROLE_PREFIX(roleID);
            TRole tRole= (TRole) modelCacheService.findByPrefixID(role_prefix);
            FastTable<ItemCell> GongBookList=player.getGongItem();   //已使用的所有功法书
            FastTable<BookModel> bookModelFastTable=new FastTable<BookModel>();   //已经装备的功法书
            for (ItemCell cell:GongBookList){
                bookModelFastTable.addLast(ScriptFactory.bookModelFastMap.get(cell.getItemTypeID()));
            }
            GongBookList.clear();
            GongBookList=null;
            double allStorePercent= 0x0;
            int allStoreLingQiLimit=0;
            for (BookModel book:bookModelFastTable){
                allStoreLingQiLimit+=book.getEffectNum1();
            }
            if (bookModelFastTable.size()!= 0x0){
                bookModelFastTable.sort();//升序
               for (;;){
                   BookModel bookModel=bookModelFastTable.getLast();
                   int groupID=bookModel.getGroupID();
                   FastTable<BookModel>tempBookList=new FastTable<BookModel>();
                   for (;;){
                       BookModel book=bookModelFastTable.getLast();
                       if (book.getGroupID()!=groupID)break;
                       bookModelFastTable.removeLast();
                       tempBookList.addLast(book);
                       if (bookModelFastTable.size()==0)break;
                   }
                   allStorePercent+= GameFormula.LingQiPercnet(tempBookList.size(), Config.DEFAULT_VALUE.GAME_VALUE.GongPercent);
                   tempBookList.clear();
                   if (bookModelFastTable.size()==0){
                       bookModelFastTable=null;
                       tempBookList=null;
                       break;
                   }
                   //to do next
               }
            }
            int allLingQiLimit=GameFormula.allLingQiLimit(allStoreLingQiLimit,allStorePercent);
            if (tRole.getStorelingqilimit()<allLingQiLimit){
                tRole.setRolefivetype(ScriptFactory.bookModelFastMap.get(_bookModel.getId()).getElement().byteValue());//更新自身角色五行
                tRole.setStorelingqilimit(allLingQiLimit);
                playerService.updateRole(tRole);
                modelCacheService.addObjectPrefixID(role_prefix,tRole);
            }
            builder.setRoleLingQiLimit(allLingQiLimit);
        }

    }

}
